package net.area80.components
{
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	import gs.easing.Strong;

	public class SnapScrollbars extends Sprite
	{
		private var scrollObject:DisplayObject;
		private var snapHeight:Number;
		private var cSnap:uint = 0;
		private var totalSnap:uint = 0;
		private var initPos:Number;
		
		private var barClip:InteractiveObject;
		private var dragClip:InteractiveObject;
		private var upClip:InteractiveObject;
		private var downClip:InteractiveObject;
		private var showPerpage:uint = 1;
		private var cRatio:Number = 0;
		
		public function SnapScrollbars() 
		{
			addEventListener(Event.REMOVED_FROM_STAGE,dispose);
			super();
		}
		public function initScrollbars (scrollObject:DisplayObject, snapHeight:Number, showPerpage:uint = 1):void {
			this.showPerpage = showPerpage;
			this.scrollObject = scrollObject;
			this.snapHeight = snapHeight;
			initPos = scrollObject.y;
			
			totalSnap = Math.ceil(scrollObject.height/snapHeight);
			cSnap = 0;
			if(totalSnap>showPerpage) {
				initButtons();
				visible =true;
			} else {
				visible =false;
			}
			
		}
		private function initButtons ():void {
			barClip = getChildByName ("bar") as InteractiveObject;
			dragClip = getChildByName ("drag") as InteractiveObject;
			upClip = getChildByName ("up") as InteractiveObject;
			downClip = getChildByName ("down") as InteractiveObject;
			dragClip.y = 0;
			if(barClip) {
				barClip.addEventListener(MouseEvent.MOUSE_DOWN, clickBarHandler);
			}
			if(dragClip) {
				dragClip.addEventListener(MouseEvent.MOUSE_DOWN, dragBarHandler);
			}
			if(upClip) {
				upClip.addEventListener(MouseEvent.CLICK, prevHandler);
			}
			if(downClip) {
				downClip.addEventListener(MouseEvent.CLICK, nextHandler);
			}
		}
		private function get barHeight ():Number {
			return barClip.height;
		}
		private function nextHandler (e:MouseEvent=null):void {
			var t:Number = 4*(1/(totalSnap-showPerpage));
			//trace("total:"+totalSnap+",t:"+t);
			var s:Number = cRatio;
			if(s<1) {
				s+= t;
				moveByRaio(Math.min(1,s));
				
			}
			
		}
		private function prevHandler (e:MouseEvent=null):void {
			
			var t:Number = 4*(1/(totalSnap-showPerpage));
			//trace("total:"+totalSnap+",t:"+t);
			var s:Number = cRatio;
			if(s>0) {
				s-= t;
				moveByRaio(Math.max(0,s));
				
			}
			
		}
		private function updateDragByRatio (r:Number):void {
			
			//var pos:Number = (Math.round(r*snapHeight)/snapHeight)*(barHeight-dragClip.height);
			var pos:Number = r*(barHeight-dragClip.height);
			TweenLite.to(dragClip, 0.3, {ease:Strong.easeOut, y:pos});
			cRatio = r;
		}
		private function moveByRaio (r:Number):void {
			var pos:Number = Math.round(r*(totalSnap-showPerpage))*snapHeight;
		
			TweenLite.to(scrollObject, 0.3, {ease:Strong.easeOut, y:initPos-pos});
			updateDragByRatio(r);
		}
		private function updateBySnap (s:Number):void {
			var t:Number = Math.ceil(totalSnap/showPerpage);
			var r:Number = (s/t);
			moveByRaio(r);
		}
		private function clickBarHandler (e:MouseEvent=null):void {
			var ratio:Number = (mouseY-barClip.y)/barClip.height;
			moveByRaio(ratio);
			//updateDragByRatio(ratio);
		}
		private function dragBarHandler (e:MouseEvent=null):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
		}
		private function moveHandler (e:MouseEvent=null):void {
			var ty:Number = Math.min(barHeight-dragClip.height,Math.max(0, mouseY));
			var ratio:Number = ty/(barHeight-dragClip.height);
			moveByRaio(ratio);
		}
		private function stopDragHandler (e:MouseEvent=null):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
		}
		private function dispose (e:Event=null):void {
			stopDragHandler();
		}
		
	}
}